﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Common;
using School.Accounting;
using School.Admission;

namespace School.Pages.Admission
{
    public partial class TeacherComments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(120) == true)
                {

                    if (!IsPostBack)
                    {
                        LstCampuses.DataSource = Structure.GetCampuses();
                        LstCampuses.DataValueField = "Id";
                        LstCampuses.DataTextField = "Name";
                        LstCampuses.DataBind();

                        LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                        LstCertificates.DataValueField = "Id";
                        LstCertificates.DataTextField = "Name";
                        LstCertificates.DataBind();

                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });

                        LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
                        LstStages.DataValueField = "Id";
                        LstStages.DataTextField = "Name";
                        LstStages.DataBind();
                        LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });

                        LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
                        LstGrades.DataValueField = "Id";
                        LstGrades.DataTextField = "Name";
                        LstGrades.DataBind();
                        LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });

                        LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue));
                        LstStudents.DataValueField = "Id";
                        LstStudents.DataTextField = "Name";
                        LstStudents.DataBind();
                        LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });

            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });

            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });

            LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue));
            LstStudents.DataValueField = "Id";
            LstStudents.DataTextField = "Name";
            LstStudents.DataBind();
            LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
            
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });

            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });

            LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue));
            LstStudents.DataValueField = "Id";
            LstStudents.DataTextField = "Name";
            LstStudents.DataBind();
            LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });           
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });

            LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue));
            LstStudents.DataValueField = "Id";
            LstStudents.DataTextField = "Name";
            LstStudents.DataBind();
            LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });            
        }

        protected void LstGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue)).Where(x => x.Status_Id > 4).Select(x => new { x.Id, Name = x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName });
            LstStudents.DataValueField = "Id";
            LstStudents.DataTextField = "Name";
            LstStudents.DataBind();
            LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });          
        }

        protected void HfStudentId_ValueChanged(object sender, EventArgs e)
        {
            long selectedStudent;

            if (long.TryParse(HfStudentId.Value, out selectedStudent))
            {
                StudentsPanel.Visible = false;
            }

            var student = Student.GetStudentById(selectedStudent).FirstOrDefault();
            lblStudentName.Text = student.Ar_FirstName + "" + student.Ar_SecondName + " " + student.Ar_ThirdNmae + " " + student.Ar_FourthName;

            long teacherId = Convert.ToInt64(Session["UserId"]);
            var data = Student.GetCommentsByTeacherIdStudentId(teacherId, selectedStudent);
            griddata.DataSource = data;
            griddata.DataBind();
            griddata.Visible = true;
            txtComment.Text = string.Empty;
            chViewOnSite.Checked = false;
        }

        protected void LstStudents_SelectedIndexChanged(object sender, EventArgs e)
        {
            HfStudentId.Value = LstStudents.SelectedValue;
            HfStudentId_ValueChanged(null, null);
        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            long id = 0;
            string name = null;

            long.TryParse(TxtStudentNumber.Text, out id);
            name = TxtStudentName.Text;

            var d = Students.Search(id, name);

            if (d.Count() > 0)
            {
                StudentsGrid.DataSource = d;
                StudentsGrid.DataBind();
                StudentsPanel.Visible = true;
                StudentsGrid.Visible = true;
                LblSearchNotFound.Visible = false;
            }
            else
            {
                StudentsPanel.Visible = true;
                LblSearchNotFound.Visible = true;
                StudentsGrid.Visible = false;
            }

            TxtStudentName.Text = "";
            TxtStudentNumber.Text = "";
        }

        protected void StudentsGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            HfStudentId.Value = ((Label)StudentsGrid.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("LblStudentId")).Text;           
            StudentsPanel.Visible = false;
            HfStudentId_ValueChanged(null, null);  
        }

        protected void lnkUpdate_Click(object sender, EventArgs e)
        {
            LinkButton lnk = (LinkButton)sender;
            long commentId = Convert.ToInt64(lnk.CommandArgument);
            var _comment = Student.GetCommentsById(commentId).FirstOrDefault();
            HfStudentId.Value = _comment.StudentId.ToString();
            hfCommentId.Value = _comment.CommentId.ToString();
            txtComment.Text = _comment.Comment;
            chViewOnSite.Checked = Convert.ToBoolean(_comment.ViewOnSite);
            btnAdd.Text = " تعديل ";
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(hfCommentId.Value))
                Student.InsertComment(chViewOnSite.Checked, txtComment.Text, Convert.ToInt64(Session["UserId"]), Convert.ToInt64(HfStudentId.Value));
            else
                Student.updateComment(Convert.ToInt64(hfCommentId.Value), chViewOnSite.Checked, txtComment.Text, Convert.ToInt64(Session["UserId"]), Convert.ToInt64(HfStudentId.Value));

            HfStudentId_ValueChanged(null, null);
            hfCommentId.Value = string.Empty;
            txtComment.Text = TxtStudentName.Text = string.Empty;
            btnAdd.Text = "اضافة الملاحظة";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            hfCommentId.Value = string.Empty;
            txtComment.Text = TxtStudentName.Text = string.Empty;
        }

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            HfStudentId_ValueChanged(null, null);
        }
        
    }
}